﻿using SmartHotel.Dto;
using SmartHotel.Menu;
using SmartHotel.RoleMenus.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace SmartHotel.RoleMenus
{
    public class SmartHotelAppRoleMenuService : ApplicationService, ISmartHotelAppRoleMenuService
    {
        public SmartHotelAppRoleMenuService(IRepository<SmartHotel.RoleMenu, int> RoleMenu)
        {
            this.RoleMenu = RoleMenu;
        }

        public IRepository<RoleMenu, int> RoleMenu { get; }

        /// <summary>
        /// 查询角色菜单
        /// </summary>
        /// <returns></returns>
        public async Task<ResultDto<List<RoleMenuDto>>> GetRoleMenuAsync()
        {
            var list = await RoleMenu.GetListAsync();
            var dto = ObjectMapper.Map<List<RoleMenu>, List<RoleMenuDto>>(list);
            return new ResultDto<List<RoleMenuDto>> { Code = Result.Success, Data = dto };
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<ResultDto<CreateandUpdateRoleMenu>> CreateRoleMenu(CreateandUpdateRoleMenu dto)
        {
            var data = await RoleMenu.GetListAsync(x => x.RoleId == dto.RoleId);
            await RoleMenu.DeleteManyAsync(data);
            var list = dto.MenuId.Select(x => new RoleMenu
            {
                RoleId = dto.RoleId,
                MenuId = x
            }).ToList();
            //var rolemenu = ObjectMapper.Map<CreateandUpdateRoleMenu, RoleMenu>(list);
            await RoleMenu.InsertManyAsync(list);
            return new ResultDto<CreateandUpdateRoleMenu> { Code = Result.Success, Message = "添加成功" };
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ResultDto<bool>> DeleteRoleMenuAssync(int id)
        {
            await RoleMenu.DeleteAsync(id);
            return new ResultDto<bool> { Code = Result.Success };
        }

        public async Task<ResultDto<List<int>>> GetFindRoleMenu(int id)
        {
            var data =await RoleMenu.GetListAsync(x => x.RoleId == id);
            var list = data.Select(x => x.MenuId).ToList();
            return new ResultDto<List<int>>
            {
                Code = Result.Success,
                Data = list
            };
        }
    }
}
